import * as address from '@/api/address.js';
const state = {
	addresses: [],
	province: uni.getStorageSync('province') || []
};
const getters = {
	addresses: state => state.addresses,
	province: state => state.province
};
const mutations = {
	SET_ADDRESSES(state, addresses) {
		state.addresses = addresses
	},
	SET_PROVINCE(state, province) {
		uni.setStorageSync('province', province);
		state.province = province
	}
};
const actions = {
	//获取用户的收货地址接口
	addressList({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			address.addressList(payload).then(res => {
				commit('SET_ADDRESSES', res.data || []);
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//添加或更新收货地址
	addressSave({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			address.addressSave(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//获取收货地址的详情
	addressDetail({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			address.addressDetail(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//删除指定的收货地址
	addreDelete({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			address.addreDelete(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//获取省份
	getAllProvince({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			address.getAllProvince(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//获取省份下的城市
	getAllCity({ commit }, payload) {
		return new Promise((resolve, reject) => {
			address.getAllCity(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//获取城市下的地区
	getAllArea({ commit }, payload) {
		return new Promise((resolve, reject) => {
			address.getAllArea(payload).then(res => {
				commit('SET_PROVINCE', res.data);
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	},
	//通过省市区获取小区列表
	communityList({ commit }, payload = {}) {
		return new Promise((resolve, reject) => {
			address.communityList(payload).then(res => {
				resolve(res)
			}).catch(err => {
				reject(err)
			})
		})
	}
};

export default { state, getters, mutations, actions };
